package tiddly;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringEscapeUtils;

import objects.Tiddler;

public class TiddlyHandler {

	private static File fileDir;
	private static File fileDir2;

	private static void update(){
		fileDir = new File("C:/Projekt/empty.html");
		fileDir2 = new File("C:/Projekt/empty2.html");
	}

	public static void writeData(String source){
		try {
			update();

			FileReader in = new FileReader(fileDir);
			BufferedReader br = new BufferedReader(in);

			BufferedWriter out = new BufferedWriter(new FileWriter(fileDir2));

			List<String> tmp = new ArrayList<String>();
			String s = null;

			while((s = br.readLine())!= null)
			{
				tmp.add(s);
			}

			for (int i=0; i<tmp.size();i++){
				if (tmp.get(i).equalsIgnoreCase("<div id=\"storeArea\">")){
					out.write(tmp.get(i)+"\n");
					out.write(source);

				}

				else {
					out.write(tmp.get(i)+"\n");
				}

			}	

			br.close();
			out.close();

			fileDir.delete();
			fileDir2.renameTo(fileDir);


		} 
		catch (IOException e) {
			e.printStackTrace();
		}


	}

	public static void readDatabase(){

		try
		{

			update();
			FileInputStream in = new FileInputStream(fileDir);
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF8"));
			String strLine;
			String tmp = null;

			while((strLine = br.readLine())!= null)
			{
				tmp += strLine;
			}
			String[] bigString = tmp.split("<div id=\"storeArea\">");
			String[] smallString = bigString[1].split("<!--POST-STOREAREA-->");
			String[] evenSmaller = smallString[0].split("<div");

			for (int i = 1; i<evenSmaller.length;i++){
				String[] database = evenSmaller[i].split("\"");

				if(database[8].equalsIgnoreCase(" tags=")){
					new Tiddler(database[1], database[3], database[5], Long.parseLong(database[7]), database[9], Integer.parseInt(database[11]), database[12]);	
				} 
				else{
					new Tiddler(database[1], database[3], database[5], Long.parseLong(database[7]), "none", Integer.parseInt(database[9]), database[10]);
				}

			}
			br.close();
		}catch(Exception e){
			e.printStackTrace();
		}

	}

	public static String encoder(String eingabe){
		String encode = StringEscapeUtils.escapeHtml4(eingabe);
		return encode;
	}

	public static void deleteTiddly(String name){
		try {
			update();

			FileReader in = new FileReader(fileDir);
			BufferedReader br = new BufferedReader(in);

			BufferedWriter out = new BufferedWriter(new FileWriter(fileDir2));

			Pattern pat = Pattern.compile("<div title=\""+name+"\"");
			Matcher mat;
			List<String> tmp = new ArrayList<String>();
			String s = null;

			while((s = br.readLine())!= null)
			{
				tmp.add(s);
			}

			for (int i=0; i<tmp.size();i++){

				mat = pat.matcher(tmp.get(i));

				if (mat.lookingAt()){
					int j = i;
					while(!tmp.get(j).equalsIgnoreCase("</div>")){
						j++;
					}
					i = j;
				}
				else {
					out.write(tmp.get(i)+"\n");
				}

			}	

			br.close();
			out.close();

			fileDir.delete();
			fileDir2.renameTo(fileDir);


		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void updateTiddlyHeader(String name, String field, String target, String update){
		try {
			update();

			FileReader in = new FileReader(fileDir);
			BufferedReader br = new BufferedReader(in);

			BufferedWriter out = new BufferedWriter(new FileWriter(fileDir2));

			Pattern pat = Pattern.compile("<div title=\""+name+"\"");
			Matcher mat;
			List<String> tmp = new ArrayList<String>();
			String s = null;

			while((s = br.readLine())!= null)
			{
				tmp.add(s);
			}

			for (int i=0; i<tmp.size();i++){

				mat = pat.matcher(tmp.get(i));

				if (mat.lookingAt()){
					String old = tmp.get(i);
					String[] split = old.split(field+"=");

					if(split.length==2){

						String replacement = split[1].replaceFirst(target, update);

						out.write(split[0]+field+"="+replacement);
					}
					else{
						System.out.println("FEHLER: Feldname inkonsistent! Der Name taucht mehrmals im Header auf.");
					}
				}
				else {
					out.write(tmp.get(i)+"\n");
				}

			}	

			br.close();
			out.close();

			fileDir.delete();
			fileDir2.renameTo(fileDir);


		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void updateTiddlyLocation(String name, String update){
		try {
			update();

			FileReader in = new FileReader(fileDir);
			BufferedReader br = new BufferedReader(in);

			BufferedWriter out = new BufferedWriter(new FileWriter(fileDir2));

			Pattern pat = Pattern.compile("<div title=\""+name+"\"");
			Pattern pat2 = Pattern.compile("Aufenthaltsort:");
			Matcher mat;
			Matcher mat2;
			List<String> tmp = new ArrayList<String>();
			String s = null;

			while((s = br.readLine())!= null)
			{
				tmp.add(s);
			}

			for (int i=0; i<tmp.size();i++){

				mat = pat.matcher(tmp.get(i));

				if (mat.lookingAt()){
					int end = i;
					
					while(!tmp.get(end).equalsIgnoreCase("</div>")){
						end++;
					}
					for (int j = i; j<end;j++){
						mat2 = pat2.matcher(tmp.get(j));

						if (mat2.lookingAt()){
							out.write("Aufenthaltsort: [["+update+"]].\n");
						}
						else{
							out.write(tmp.get(j)+"\n");
						}
					}
					i = end-1;
				}


				else {
					out.write(tmp.get(i)+"\n");
				}

			}	

			br.close();
			out.close();

			fileDir.delete();
			fileDir2.renameTo(fileDir);


		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void updateTiddlyBesitzer(String name, String update){
		try {
			update();

			FileReader in = new FileReader(fileDir);
			BufferedReader br = new BufferedReader(in);

			BufferedWriter out = new BufferedWriter(new FileWriter(fileDir2));

			Pattern pat = Pattern.compile("<div title=\""+name+"\"");
			Pattern pat2 = Pattern.compile("Besitzer:");
			Matcher mat;
			Matcher mat2;
			List<String> tmp = new ArrayList<String>();
			String s = null;

			while((s = br.readLine())!= null)
			{
				tmp.add(s);
			}

			for (int i=0; i<tmp.size();i++){

				mat = pat.matcher(tmp.get(i));

				if (mat.lookingAt()){
					int end = i;
					
					while(!tmp.get(end).equalsIgnoreCase("</div>")){
						end++;
					}
					for (int j = i; j<end;j++){
						mat2 = pat2.matcher(tmp.get(j));

						if (mat2.lookingAt()){
							out.write("Besitzer: [["+update+"]].\n");
						}
						else{
							out.write(tmp.get(j)+"\n");
						}
					}
					i = end-1;
				}


				else {
					out.write(tmp.get(i)+"\n");
				}

			}	

			br.close();
			out.close();

			fileDir.delete();
			fileDir2.renameTo(fileDir);


		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}
